﻿using Core.Context;
using Core.Dtos;
using Core.Domain;
using Core.Factories;
using Core.IServices;
using Core.Messages;
using Core.Queries;
using Library.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using Share.Enterprise.Api;

namespace Front.Areas.Admin.Controllers.Api
{
    [RoutePrefix("api/admin/users")]        
    //[Authorize(Roles = "Admin")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class UserApiController : ApiController, ICrudApiController<string, UserDto, UserQuery>, IQueryApiController<string, UserDto, UserQuery>
    {
        private readonly IManageUserService service;
        private readonly IRequestFactory requestFactory;
        private readonly ICurrentContext currentContext;
      
        public UserApiController(IManageUserService service, ILogger logger, ICurrentContext currentContext, IRequestFactory requestFactory)           
        {
            this.service = service;            
            this.requestFactory = requestFactory;
            this.currentContext = currentContext;
        }

        [HttpPost]
        [Route("searches")]
        public QueryResponse<UserDto> FindBy(UserQuery query)
        {
            Request<UserQuery> request = requestFactory.GetInstance<UserQuery>(query);
            return service.FindBy(request);
        }

        [Route("{id}")]
        public UserDto Get(string id)
        {
            return service.FindByID(id);
        }

        [HttpPost]
        [Route("")]
        public virtual Response<string> Post(UserDto dto)
        {
            Request<UserDto> request = requestFactory.GetInstance<UserDto>(dto);
            return service.Add(request);
        }

        [HttpPut]
        [Route("{id}")]
        public virtual Response Put(string id, UserDto dto)
        {
            Request<UserDto> request = requestFactory.GetInstance<UserDto>(dto);
            return service.Edit(id, request);
        }

        [HttpDelete]
        [Route("{id}")]
        public virtual Response Delete(string id)
        {
            return service.Delete(id);
        }
    } 
}
